
Dim session As NotesSession
Dim db As notesDatabase
Dim uidoc As NotesUIDocument
Dim doc As NotesDocument
Dim docA As NotesDocument
Dim docB As NotesDocument
Dim x As Integer
Dim y As Integer
Dim i As Integer

Sub Initialize
	
	Dim workspace As New NotesUIWorkspace
	Set session = New NotesSession
	Set db = session.CurrentDatabase
	
End Sub

Sub RepeatDaily (DocRef As NotesDocument)
	
	Dim workspace As New NotesUIWorkspace   
	Dim dtWeekend As Integer
	Set uidoc = workspace.CurrentDocument  
	Set docA = uidoc.Document
	
	' Msgbox "Repeat on daily basis."
	Dim dtValue As New NotesDateTime( docA.StartDate(0) )
	x% = docA.RepeatDateDiff(0)
	
	For i = 1 To x
		Call dtValue.AdjustDay( i )
		dtWeekend = Weekday ( dtValue.DateOnly ) 
		If dtWeekend = 1 Or dtWeekend = 7 Then
			Select Case docA.RepeatWeekends(0)
			Case "X"  : REM "place holder for future use"
			Case "D"  : Call CreateRepeatEntry ( dtValue.DateOnly, DocRef )	
			Case "F"  : REM "place holder for future use"
			Case "M"
				If dtWeekend = 7 Then
					If x < i+2 Then
						Call dtValue.AdjustDay( 2 )
						Call CreateRepeatEntry ( dtValue.DateOnly, DocRef )
						Call dtValue.AdjustDay( -2 )
					End If
				End If
			Case Else : Msgbox "We should not be here... (RepeatDaily)"
			End Select
		Else
			Call CreateRepeatEntry ( dtValue.DateOnly, DocRef )                
		End If
		Print "Creating repeat reservations... ( " & i & " of " & x &  ")"
		Call dtValue.AdjustDay( -i )
	Next
	
End Sub

Sub RepeatWeekly (DocRef As NotesDocument)
	
	Dim workspace As New NotesUIWorkspace   
	Dim dtWeekday As Integer
	Set uidoc = workspace.CurrentDocument  
	Set docA = uidoc.Document
	
	' Msgbox "Repeat on weekly basis."
	Dim dtValue As New NotesDateTime( docA.StartDate(0) )
	x% = docA.RepeatDateDiff(0)
	For i = 1 To x 
		Call dtValue.AdjustDay( i )
		dtWeekday = Weekday ( dtValue.DateOnly )                                                      
		Forall d In docA.RepeatAdjust_W                                                                        
			If d = Cstr( dtWeekday - 1) Then Call CreateRepeatEntry ( dtValue.DateOnly, DocRef) 
		End Forall                                                                                                             	
		Call dtValue.AdjustDay( -i )                                                                                   	
	Next
	
End Sub

Sub RepeatMonthlyDay (DocRef As NotesDocument)
	
	Dim workspace As New NotesUIWorkspace
	Dim dtWeek As Integer
	Dim dtWeekday As Integer
	Dim dtDay As Integer
	Dim tmpDate As String	
	Dim indicator As String
	Dim category As NotesItem
	
	Set uidoc = workspace.CurrentDocument  
	Set docA = uidoc.Document 
	Set category =  docA.GetFirstItem ("RepeatAdjust_MP")
	tmpDate$ = Month(docA.StartDate(0)) & "/01/" & Year(docA.StartDate(0))
	Dim dtValue As New NotesDateTime( tmpDate$ )
	
	' Msgbox "Repeat on monthly by day basis."
	dtWeek = 1          
	Do Until Cdat (dtValue.DateOnly) > Cdat (docA.RepeatUntil(0))
		dtWeekday = Weekday ( dtValue.DateOnly )
		For dtDay = 0 To 6
			indicator = dtWeek & "." & (Weekday(dtValue.DateOnly) -1)
			If category.Contains( indicator ) Then
				If Datevalue(dtValue.DateOnly) > Datevalue(docA.StartDate(0)) Then 
					Call CreateRepeatEntry ( dtValue.DateOnly, DocRef )			
				End If
			End If
			Call dtValue.AdjustDay( 1 )
			If Day (dtValue.DateOnly) = 1 Then 
				dtWeek = 0
				Exit For
			End If
		Next
		dtWeek = dtWeek + 1 
	Loop
	
End Sub

Sub RepeatMonthlyDate   (DocRef As NotesDocument)
	
	Dim workspace As New NotesUIWorkspace
	Dim dtWeek As Integer
	Dim dtWeekday As Integer
	Dim dtDay As Integer
	Dim tmpDate As String	
	Dim Indicator As String
	Dim category As NotesItem
	
	Set uidoc = workspace.CurrentDocument  
	Set docA = uidoc.Document
	
	' Msgbox "Repeat on monthly by date basis."
	Dim dtValue As New NotesDateTime( docA.StartDate(0) )
	x% = docA.RepeatDateDiff(0)
	For i = 1 To x       
		Call dtValue.AdjustDay( i )
		Forall d In docA.RepeatAdjust_MD 
			dtDay = Day ( dtValue.DateOnly )
			If Cint(d) = dtDay Then
				dtWeekend = Weekday ( dtValue.DateOnly )
				If dtWeekend = 1 Or dtWeekend = 7 Then
					Select Case docA.RepeatWeekends(0)
					Case "X"  : Continue = 1
					Case "D" : Call CreateRepeatEntry ( dtValue.DateOnly, DocRef)
					Case "F"  : Continue = 1
						Set dtRange = docA.GetFirstItem( "RepeatAdjust_MD" )                              
						If dtWeekend = 1 Then
							Call dtValue.AdjustDay( -2 )
							ddd$ = Day ( dtValue.DateOnly )
							If Not dtRange.Contains( ddd$ ) Then Call CreateRepeatEntry ( dtValue.DateOnly, DocRef)
							Call dtValue.AdjustDay( 2 ) 
						Else
							Call dtValue.AdjustDay( -1 )
							ddd$ = Day ( dtValue.DateOnly )
							If Not dtRange.Contains( ddd$ ) Then Call CreateRepeatEntry ( dtValue.DateOnly, DocRef)
							Call dtValue.AdjustDay( 1 ) 
						End If
					Case "M"
						Set dtRange = docA.GetFirstItem( "RepeatAdjust_MD" )
						If dtWeekend = 1 Then
							Call dtValue.AdjustDay( 1 )
							ddd$ = Day ( dtValue.DateOnly )
							If Not dtRange.Contains( ddd$ ) Then Call CreateRepeatEntry ( dtValue.DateOnly, DocRef)
							Call dtValue.AdjustDay( -1 ) 
						Else
							Call dtValue.AdjustDay( 2 )
							ddd$ = Day ( dtValue.DateOnly )
							If Not dtRange.Contains( ddd$ ) Then Call CreateRepeatEntry ( dtValue.DateOnly, DocRef)
							Call dtValue.AdjustDay( -2 ) 
						End If
					Case Else : Msgbox "We should not be here... (RepeatMonthlyDate)"
					End Select
				Else
					Call CreateRepeatEntry ( dtValue.DateOnly, DocRef)
				End If
				Exit Forall
			End If               
		End Forall
		Call dtValue.AdjustDay( -i )
	Next
	
End Sub

Sub RepeatCustom (DocRef As NotesDocument)
	
	Dim workspace As New NotesUIWorkspace     
	Set uidoc = workspace.CurrentDocument  
	Set docA = uidoc.Document
	
	' Msgbox "Repeat on custom basis."  
	x = 1
	y = Ubound(docA.RepeatDates) + 1
	Forall i In docA.RepeatDates
		Print "Creating repeat reservations... ( " & x & " of " & y &  ")"
		Call CreateRepeatEntry ( i, DocRef) 
		x = x + 1
	End Forall
	
End Sub

Sub ProcessRepeatType (DocRef As NotesDocument)
	
	Dim workspace As New NotesUIWorkspace     
	Set uidoc = workspace.CurrentDocument  
	Set docA = uidoc.Document
	
	If docA.RepeatUnit(0) <> "" Then 
		Select Case docA.RepeatUnit(0)
		Case "D" :      Call RepeatDaily (DocRef)
		Case "W" :     Call RepeatWeekly (DocRef)
		Case "MP"  :  Call RepeatMonthlyDay  (DocRef)
		Case "MD" :   Call RepeatMonthlyDate  (DocRef)
		Case "C" :      Call RepeatCustom  (DocRef)
		Case Else:      Msgbox "Repeat Unit error in ScriptLibrary"
		End Select
		Print "Repeat reservations complete"   
	End If
	
End Sub

Sub DisplayRepeatDialog
	
	Dim workspace As New NotesUIWorkspace
	Set uidoc = workspace.CurrentDocument 
	Set doc = uidoc.Document
	Call workspace.DialogBox( "(Res)", True, True, , , , , "Repeat Reservation" )    
	
End Sub

Sub CreateRepeatEntry ( RepeatDateValue As Variant , DocRef As NotesDocument)    
	
	Dim workspace As New NotesUIWorkspace     
	Dim dateRange As NotesDateRange
	Dim sdate As String
	Dim repeatDate As String	
	Dim date1 As NotesDateTime
	Dim theRepeatDate As NotesDateTime
	Dim theStartDate As NotesDateTime
	Dim rStartDate As String
	
	Set uidoc = workspace.CurrentDocument  
	Set docA = uidoc.Document
	Set docB = New NotesDocument( db )
	Set dateRange = session.CreateDateRange()    
	
	'-----------------------------------------------------    
	' Make copy of original reservation
	'-----------------------------------------------------    
	Call docA.CopyAllItems( docB, True )     
	
	'-----------------------------------------------------    
	' Change the date on the new repeat entry     
	'-----------------------------------------------------    
	dateRange.Text = uidoc.FieldGetText( "TimeRange" )
	sdate$ = Datevalue ( RepeatDateValue ) & " " & dateRange.StartDateTime.LocalTime
	Set date1 = New NotesDateTime( sdate$ )
	docB.StartDateTime = date1.LSLocalTime
	docB.StartDate = date1.LSLocalTime
	
	'----------------------------------------------------- 
	' Cleanup/Reset various data fields
	'----------------------------------------------------- 
	docB.RemoveItem("Invite") 
	docB.ReservationStatus = "CLOSED"
	Call docB.Save( True, True, True )
	
	'-----------------------------------------------------
	' Set the "repeat date instances" in the main record. 
	' Required to process repeat appt in LN email client.
	'-----------------------------------------------------
	Dim oldArray
	Dim newArray() As Variant
	rStartDate$ = Datevalue (DocRef.StartDate(0)) & " " & docRef.Time1(0)
	Set theStartDate = New NotesDateTime ( rStartDate$ ) 
	repeatDate$ = Datevalue (RepeatDateValue)  & " " & docRef.Time1(0)
	Set theRepeatDate = New NotesDateTime ( repeatDate$ ) 
	If DocRef.RepeatInstanceDates(0) = "" Then DocRef.RepeatInstanceDates = theStartDate.LSLocalTime
	oldArray = docRef.RepeatInstanceDates
	Redim Preserve newArray(Ubound(oldArray)+1)
	newArray(0) = theRepeatDate.LSLocalTime
	For x = 1 To Ubound(newArray)
		newArray(x) = oldArray(x-1)		
	Next
	docRef.RepeatInstanceDates = newArray
	
End Sub

Function CheckFieldValues ( DocRef As NotesDocument )
	
	CheckFieldValues = True
	MsgText = ""
	If DocRef.Topic(0) = "" Then
		MsgText = MsgText + "Specify a topic." + Chr$(13)
	End If
	
	If DocRef.StartDate(0) = "" Then
		MsgText = MsgText + "Specify a start date." + Chr$(13)
	End If
	
	If DocRef.Time1(0) = "" Then
		MsgText = MsgText + "Specify a start time." + Chr$(13)
	End If
	
	If DocRef.Time2(0) = "" Then
		MsgText = MsgText + "Specify an end time." + Chr$(13)
	End If
	
	If MsgText <> "" Then
		Msgbox MsgText,16,"Required Fields."
		CheckFieldValues = False
	End If
	
End Function
